package events;

import java.util.ArrayList;
import java.util.List;

import core.Blog;
import core.Post;

/**
 * This event is thrown when a Blog update is finished.
 * 
 * @author ole
 */
public class UpdateCompleteEvent extends BlogEvent {
	// Class constants.
	private static final long serialVersionUID = 1L;
	
	// Class attributes.
	private List<Post> new_posts;

	/**
	 * Class constructor.
	 * 
	 * @param source:
	 * 		The Blog that generated this event.
	 */
	public UpdateCompleteEvent(Blog source) {
		super(source);
		new_posts = new ArrayList<Post>();
	}
	
	/**
	 * When this Event is created it has no Post associated to the update, we
	 * have to add those Posts that had been added with this update to the
	 * event, in order that the user of the Event can know whose Posts are the
	 * newer Posts.
	 * 
	 * It is checked that the Post we are adding to this event exists inside
	 * the posts of the Blog.
	 * 
	 * @param post:
	 * 		A new Post that had been added to the Blog.
	 * @throws Exception:
	 * 		Thrown when tried to add a Post that isn't inside the Blog.
	 */
	public void addNewPost(Post post) throws Exception {
		Blog blog;
		
		blog = (Blog)source;
		if (!blog.getPosts().contains(post))
			throw new Exception("UpdateCompleteEvent: Tried to add to the " +
								"event a Post that isn't inside the Blog " +
								"updated.");
			
		new_posts.add(post);
	}
	
	/**
	 * Same as the addNewPost() method but with a List of Posts. It adds each
	 * of the Posts inside the List to the this Event.
	 * 
	 * @param post:
	 * 		A new Post that had been added to the Blog.
	 * @throws Exception:
	 * 		Thrown when tried to add a Post that isn't inside the Blog.
	 */
	public void addNewPosts(List<Post> list) throws Exception {
		for (Post post: list)
			addNewPost(post);
	}
	
	/**
	 * Returns the List of Posts added to the Blog associated to this Event.
	 */
	public List<Post> getPostsAdded() {
		return new_posts;
	}
	
	/**
	 * Returns the Blog that generated this event.
	 * 
	 * @return:
	 * 		The Blog just updated.
	 */
	@Override
	public Blog getSource() {
		return (Blog)source;
	}
}
